﻿using System.Windows.Media;
using System;
using DeepEarth.Toolkit.Geometry;
using DeepEarth.Core;
using DeepEarth.Toolkit.Geometry.Data;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace ExampleApplication.Utilities
{
    public static class Utilities
    {
        public static LineString RandomLine(Random rng, int points)
        {
            points = Math.Max(points, 2);
            LineString ls = new LineString();
            ls.Locations = RandomLocations(rng, points, false);

            return ls;
        }

        public static LineStringGeometry RandomLineStringGeometry(Random rng, int points)
        {
            points = Math.Max(points, 2);
            LineStringGeometry ls = new LineStringGeometry();
            ls.Locations = RandomLocations(rng, points, false);

            return ls;
        }

        public static Polygon RandomPolygon(Random rng, int points)
        {
            points = Math.Max(points, 4);
            Polygon p = new Polygon();
            p.OuterRing = RandomLocations(rng, points, true);

            return p;
        }

        public static PolygonGeometry RandomPolygonGeometry(Random rng, int points)
        {
            points = Math.Max(points, 4);
            PolygonGeometry p = new PolygonGeometry();
            p.OuterRing = RandomLocations(rng, points, true);

            return p;
        }


        static ObservableCollection<Location> RandomLocations(Random rng, int points, bool close)
        {
            ObservableCollection<Location> locations = new ObservableCollection<Location>();

            int pointsToGenerate = close ? points - 1 : points;

            for (int i = 0; i < pointsToGenerate; i++)
            {
                double lat1 = rng.Next(-85, 85);
                double lon1 = rng.Next(-180, 180);

                locations.Add(new Location(lat1, lon1));
            }

            if (close)
            {
                locations.Add(locations[0].Clone());
            }

            return locations;
        }

        public static SolidColorBrush RandomStroke(Random rng)
        {
            byte a, r, g, b;

            a = (byte)rng.Next(100, 255);
            r = (byte)rng.Next(0, 255);
            g = (byte)rng.Next(0, 255);
            b = (byte)rng.Next(0, 255);

            return new SolidColorBrush(Color.FromArgb(a, r, g, b));
        }
    }
}
